package top.lc951.myandroid;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import top.lc951.myandroid.model.Cheeses;

/**
 * ListView的吸顶效果2
 * 布局：{@link ListView}
 */
public class ListViewStickyHeaderActivity2 extends AppCompatActivity {
    public static final String TAG = ListViewStickyHeaderActivity2.class.getSimpleName();

    private ListView mListView;//自定义的listview
    private ImageView mIvHeader;//顶部用来放大的的图片
    private TextView header_title;//顶部用来放大的的图片
    private MyAdapter myAdapter;
    private float scrollY;
    private float stopY;

    public static void actionActivity(Context context) {
        Intent intent = new Intent(context, ListViewStickyHeaderActivity2.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_sticky_header2);

        mListView = findViewById(R.id.plv);
        header_title = findViewById(R.id.header_title);
        header_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ListViewStickyHeaderActivity2.this
                        , header_title.getText().toString()
                        , Toast.LENGTH_SHORT).show();
            }
        });
        myAdapter = new MyAdapter();
        mListView.setAdapter(myAdapter);
        /*
         * 添加一个滚动 监听
         * 用来实现标题置顶效果
         * */
        mListView.setOnScrollListener(new ListView.OnScrollListener() {


            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                switch (scrollState) {
                    case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://拖动
                    case AbsListView.OnScrollListener.SCROLL_STATE_FLING://惯性滑动
                        // 显示滑动时屏幕可见条目中离标题栏最近的第一行
                        int position = mListView.getFirstVisiblePosition();
                        //由于listview添加了一个header，所以position=0代表的是header
                        //这里获取到的position和下面onscroll方法里的firstVisibleItem值一致
                        float scrollY=view.getY();

                        Log.i(TAG, "offY="+ (scrollY-stopY));
                        break;
                    case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://手指离开或者惯性滑动停止
                        stopY = view.getY();
                        break;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                Log.i(TAG, "firstVisibleItem="+firstVisibleItem
               + ",visibleItemCount="+visibleItemCount
               + ",totalItemCount="+totalItemCount
                );
                String[] title=myAdapter.getItem(firstVisibleItem);
                String currentText=header_title.getText().toString().trim();
                View scrollView=view.getChildAt(firstVisibleItem);


                if(TextUtils.isEmpty(currentText)){
                    header_title.setText(title[1]);
                }else
                    if(!title[1].equals(currentText==null?"":currentText)){
                        if(null==scrollView)
                            return;
                        float needScrollY = scrollView.getY();
                        float offY=needScrollY-scrollY;
//                        header_title.setTranslationY(offY);
                        Log.i(TAG, "offY="+ offY);
                }else{
                    //nothing
                        scrollY = scrollView.getY();
                        header_title.setTranslationY(0);
                }
                header_title.setText(title[1]);
            }

        });

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return Cheeses.data.length;
        }


        @Override
        public long getItemId(int position) {
            return 0;
        }


        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getViewTypeCount() {
            return 1;
        }

        @Override
        public String[] getItem(int position) {

            return Cheeses.data[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int type = getItemViewType(position);
            View view = null;
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();
                view = View.inflate(getBaseContext(), R.layout.list_view2_item, null);
                holder.titile = (TextView) view.findViewById(R.id.header_title);
                holder.name = (TextView) view.findViewById(R.id.tv_info);
                view.setTag(holder);
            } else {
                view = convertView;
                holder = (ViewHolder) view.getTag();
            }
            if (0 == position || !getItem(position)[1].equals(getItem(position - 1)[1])) {
                holder.titile.setVisibility(View.VISIBLE);
            } else {
                holder.titile.setVisibility(View.GONE);
            }
            holder.titile.setText(getItem(position)[1]);
            holder.titile.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(ListViewStickyHeaderActivity2.this
                            , getItem(position)[1]
                            , Toast.LENGTH_SHORT).show();
                }
            });
            holder.name.setText(getItem(position)[0]);
            return view;
        }
    }

    class ViewHolder {
        TextView titile;
        TextView name;
    }
}